import Eureka

//修改简历表单
class updateResumeObjController : FormViewController {

    private let waiting=waitingAlertUtil(msg: "请稍后..")

    //传过来的resumeId
    var resume:detailResume = detailResume()

	override func viewDidLoad() {
        super.viewDidLoad()

        // 日期转换器
        let DateFormatter=NSDateFormatter()
        DateFormatter.dateFormat="yyyy-MM-dd"

        URLRow.defaultCellUpdate = { cell, row in cell.textField.textColor = .blueColor() }
        LabelRow.defaultCellUpdate = { cell, row in cell.detailTextLabel?.textColor = .orangeColor()  }
        CheckRow.defaultCellSetup = { cell, row in cell.tintColor = .orangeColor() }
        // 设置最大最小日期
        DateRow.defaultRowInitializer = { row in 
            row.maximumDate = DateFormatter.dateFromString("2015-12-31")
            row.minimumDate = DateFormatter.dateFromString("1950-01-01")
        }

        form +++ Section()
            <<< PushRow<String>("jobNature"){
                $0.title = "工作性质"
                $0.selectorTitle = "选择你的工作性质"
                var array=enumUtil.getOrderedStringArray(enumUtil.RESUME_WORKNATURE)
                $0.options = array
                $0.value = array[0]
            }
            <<< PushRow<String>("expectSalary"){
                $0.title = "薪资范围"
                $0.selectorTitle = "选择你期望的薪资范围"
                var array=enumUtil.getOrderedStringArray(enumUtil.RESUME_SALARY_RANGE)
                $0.options = array
                $0.value = array[0]
            }
            <<< PushRow<String>("expectIndustryType"){
                $0.title = "期望行业"
                $0.selectorTitle = "期望行业"
                var array=enumUtil.getOrderedStringArray(enumUtil.INDUSRTY_ENUM)
                $0.options = array
                $0.value = array[0]
            }
            +++ Section("期望职位")
            <<< PushRow<String>("jobParent"){
                $0.title = "职位大类"
                $0.selectorTitle = "职位大类"
                let array=enumUtil.getOrderedStringArray(enumUtil.JOB_FUNC_PARENT)
                $0.options = array
                $0.value = ""
                
                // 初始职位
                $0.onChange{row in
                    if(row.value != nil){
                        print("更改职业大类")
                        let jobRow:PushRow<String>=self.form.rowByTag("expectJob")!
                        let jobID=enumUtil.getKeyByString(row.value!, dict: enumUtil.JOB_FUNC_PARENT)
                        print(enumUtil.JOB_CHILDS)
                        print(jobID)
                        var littleArray=enumUtil.getOrderedStringArray(enumUtil.JOB_CHILDS[jobID]!)
                        jobRow.options = littleArray
                        jobRow.value = littleArray[0]
                        jobRow.updateCell()
                        
                    }
                }
            }
            <<< PushRow<String>("expectJob"){
                $0.title = "职位小类"
                $0.selectorTitle = "职位小类"
            }
            +++ Section("期望工作地点")
            <<< PushRow<String>("dream_province"){
                $0.title = "省份"
                $0.selectorTitle = "省份"
                let array=enumUtil.getOrderedStringArray(enumUtil.PROVINCE)
                $0.options = array
                $0.value = ""
                
                //初始化城市
                $0.onChange{row in
                    // 初始化城市
                    if(row.value != nil){
                        let cityRow:PushRow<String>=self.form.rowByTag("dream_city")!
                        let provinceID=enumUtil.getKeyByString(row.value!, dict: enumUtil.PROVINCE)
                        var littleArray=enumUtil.getOrderedStringArray(enumUtil.PROVINCE_CITYS[provinceID]!)
                        cityRow.options = littleArray
                        cityRow.value = littleArray[0]
                        cityRow.updateCell()
                    }
                }
            }
            <<< PushRow<String>("dream_city"){
                $0.title = "市"
                $0.selectorTitle = "城市"
            }
            
            +++ Section()
            <<< ButtonRow() { (row: ButtonRow) -> Void in
                   row.title = "保存"
                row.cell.tintColor = UIColor.hexStringToColor("#ffffff")
                row.cell.backgroundColor = UIColor.hexStringToColor("#ff4081")
                }  .onCellSelection({ (cell, row) in
                    self.submitForm()
                })
        
        loadData()

    }

//  加载用户基本信息
    private func loadData(){
        // 等待提示
        waiting.show(self)
        
        var pr:PushRow<String>

        pr=form.rowByTag("jobNature")!
        pr.value=enumUtil.getStringByKey(Int(resume.JobNatureCode)!, dict: enumUtil.RESUME_WORKNATURE)
        pr=form.rowByTag("expectSalary")!
        pr.value=enumUtil.getStringByKey(Int(resume.ExpectSalaryCode)!, dict: enumUtil.RESUME_SALARY_RANGE)
        pr=form.rowByTag("expectIndustryType")!
        pr.value=enumUtil.getStringByKey(Int(resume.ExpectIndustryType)!, dict: enumUtil.INDUSRTY_ENUM)
        
//        设置城市
        let cityCode:Int = Int(resume.ExpectLocation)!
        let provinceCode:Int = enumUtil.getParentCodeByChildCode(cityCode)
        let provinceName=enumUtil.getStringByKey(provinceCode, dict: enumUtil.PROVINCE)
        let cityList = enumUtil.PROVINCE_CITYS[provinceCode]
        let cityName = enumUtil.getStringByKey(cityCode, dict: cityList!)
        
        print("更改城市")
        print(provinceName + ":"+String(provinceCode))
        print(cityName + ":"+String(cityCode))
        pr=form.rowByTag("dream_province")!
        pr.value=provinceName
        pr=form.rowByTag("dream_city")!
        pr.options = enumUtil.getOrderedStringArray(cityList!)
        pr.value = cityName
        
//        设置大类小类
        let jobCode:Int = Int(resume.ExpectJob)!
        let jobParentCode:Int = enumUtil.getParentCodeByChildCode(jobCode)
        let jobParentName=enumUtil.getStringByKey(jobParentCode, dict: enumUtil.JOB_FUNC_PARENT)
        let jobList = enumUtil.JOB_CHILDS[jobParentCode]
        let jobName = enumUtil.getStringByKey(jobCode, dict: jobList!)
        
        print("更改职业")
        print(jobParentName + ":"+String(jobParentCode))
        print(jobName + ":"+String(jobCode))
        
        pr=form.rowByTag("jobParent")!
        pr.value=jobParentName
        pr=form.rowByTag("expectJob")!
        pr.options = enumUtil.getOrderedStringArray(jobList!)
        pr.value = jobName

        
        waiting.stop()
    }

    private func submitForm(){
        // 等待提示
        waiting.show(self)
        
        
        //获取表单结果
        let dict = form.values()
        
        if(dict["dream_province"] == nil || dict["dream_city"] == nil || dict["jobParent"] == nil || dict["expectJob"] == nil){
            warningAlertUtil(title: "错误", msg: "未填写完整").show(self)
            return
        }
        
        let expectIndustryType = String( enumUtil.getKeyByString(dict["expectIndustryType"] as! String,dict: enumUtil.INDUSRTY_ENUM) )
        let jobNature = enumUtil.getKeyByString(dict["jobNature"] as! String,dict: enumUtil.RESUME_WORKNATURE)
        let expectSalary = enumUtil.getKeyByString(dict["expectSalary"] as! String,dict: enumUtil.RESUME_SALARY_RANGE)
        let expectprovince = enumUtil.getKeyByString(dict["dream_province"] as! String,dict: enumUtil.PROVINCE)
        let expectLocation = String( enumUtil.getKeyByString(dict["dream_city"] as! String,dict: enumUtil.PROVINCE_CITYS[expectprovince]!) )
        let jobParent = enumUtil.getKeyByString(dict["jobParent"] as! String,dict: enumUtil.JOB_FUNC_PARENT)
        let expectJob = String( enumUtil.getKeyByString(dict["expectJob"] as! String,dict: enumUtil.JOB_CHILDS[jobParent]!) )
        
        resumeManager.UpdateJobObjective(resume.ResumeId,
                             expectIndustryType:expectIndustryType,
                             expectjob:expectJob,
                             expectlocation:expectLocation,
                             jobnature:jobNature,
                             expectSalary:expectSalary,
                             successCallback: { (msg) in
                    //            添加信息ok
                                self.waiting.stop()
                    //            退出
                                self.navigationController?.popViewControllerAnimated(true)
                                }) { (error) in
                                    self.waiting.stop()
                                    warningAlertUtil(title: "错误", msg: error).show(self)
                            }
    }

}


